package delegate;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import manager.FileManager;
import model.DataManagerModel;

/**
 * Class       - OpenRecipeViewController
 * Description - Application window to open and view a recipe
 * Notes       - First iteration for Lunchbox GUI, keeping it simple using the MVC design concept
 * @author Ryne Okimoto
 * 
 * Inspired by Fred Swartz December 2004
 */

@SuppressWarnings("serial")

public class OpenRecipeViewController extends JFrame 
{
	// Class data
	private JPanel pnlScale;
	private JTextArea txaViewer;
	private JTextField txtMultiplier;
	private JLabel lblImage;
	private JButton btnEdit;
	private JButton btnScale;
	private JButton btnClose;
	private JButton btnConversion;
	private FileManager recipeFileManager;
	private DataManagerModel dataModel;
	
	/**
	 * Constructor
	 * @param recipe
	 * @param model
	 */
	public OpenRecipeViewController(DataManagerModel model, FileManager recipe)
	{
		pnlScale = new JPanel();
		recipeFileManager = recipe;
		dataModel = model;
		JPanel view = new JPanel();
		Image rcpImage = recipe.openImage();
		txaViewer = new JTextArea();
		txtMultiplier = new JTextField(3);
		btnEdit = new JButton("Edit recipe");
		btnScale = new JButton("Scale recipe");
		btnClose = new JButton("Close");
		btnConversion = new JButton("Unit conversion");
		
		btnConversion.setToolTipText("Converts between standard and metric units");
		pnlScale.add(txtMultiplier);
		pnlScale.add(btnScale);
		
		// For initial implementation add text to a JTextArea
		txaViewer.setText(recipe.createString());
		txaViewer.setEditable(false);
		txaViewer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		if(rcpImage != null){
			lblImage = new JLabel(new ImageIcon(rcpImage.getScaledInstance(200, 200, Image.SCALE_SMOOTH)));
		}
		else{
			lblImage = new JLabel();
			lblImage.setText("Image not available");
		}
		
		// Set layout
		view.setLayout(new GridBagLayout());
		this.setResizable(false);
		
		// Set constraints
		GridBagConstraints gbcTxaViewer = new GridBagConstraints();
		gbcTxaViewer.gridx = 0;
		gbcTxaViewer.gridy = 1;
		gbcTxaViewer.insets = new Insets(20, 20, 20, 20);
		
		GridBagConstraints gbcScale = new GridBagConstraints();
		gbcScale.gridx = 0;
		gbcScale.gridy = 3;
		gbcScale.insets = new Insets(5, 10, 0, 10);
		
		GridBagConstraints gbcBtnConv = new GridBagConstraints();
		gbcBtnConv.insets = new Insets(5, 10, 10, 10);
		gbcBtnConv.gridx = 0;
		gbcBtnConv.gridy = 4;
		
		GridBagConstraints gbcImage = new GridBagConstraints();
		gbcImage.gridx = 1;
		gbcImage.gridy = 1;
		gbcImage.insets = new Insets(0, 0, 0, 20);
		
		GridBagConstraints gbcBtnEdit = new GridBagConstraints();
		gbcBtnEdit.gridx = 1;
		gbcBtnEdit.gridy = 3;
		gbcBtnEdit.insets = new Insets(5, 10, 0, 10);
		
		GridBagConstraints gbcBtnClose = new GridBagConstraints();
		gbcBtnClose.insets = new Insets(5, 10, 10, 10);
		gbcBtnClose.gridx = 1;
		gbcBtnClose.gridy = 4;
		
		// Set listeners
		btnEdit.addActionListener(new EditClickedListener());
		btnScale.addActionListener(new ScaleClickedListener());
		btnConversion.addActionListener(new ConvertClickedListener());
		btnClose.addActionListener(new CloseClickedListener());
		
		// Add components
		view.add(txaViewer, gbcTxaViewer);
		view.add(lblImage, gbcImage);
		view.add(btnEdit, gbcBtnEdit);
		view.add(btnConversion, gbcBtnConv);
		view.add(btnClose, gbcBtnClose);
		view.add(pnlScale, gbcScale);
		
		this.setContentPane(view);
		 
		this.setTitle(recipe.getData().getRecipeName());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.pack();
	} // constructor
	
	/**
	 * Class - EditClickedListener
	 * Description - 
	 *
	 */
	private class EditClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			EditViewController editView = new EditViewController(dataModel, recipeFileManager);
			OpenRecipeViewController.this.dispose();
			editView.setVisible(true);
		}
	}
	
	private class ScaleClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			// TODO no input checking on multiplier
			// Receive boolean return value
			OpenRecipeViewController.this.recipeFileManager.getData().scaleAmount(
					Integer.parseInt(OpenRecipeViewController.this.txtMultiplier.getText()));
			
			// Refresh view
			OpenRecipeViewController.this.txaViewer.setText(
					OpenRecipeViewController.this.recipeFileManager.createString());
			OpenRecipeViewController.this.txaViewer.revalidate();
			OpenRecipeViewController.this.repaint();
		}
	}
	
	private class ConvertClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			OpenRecipeViewController.this.recipeFileManager.getData().convertUnits();
			
			// Refresh view
			OpenRecipeViewController.this.txaViewer.setText(
					OpenRecipeViewController.this.recipeFileManager.createString());
			
			OpenRecipeViewController.this.txaViewer.revalidate();
			OpenRecipeViewController.this.repaint();
		}
	}
	
	private class CloseClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			OpenRecipeViewController.this.dispose();
		}
	}
	
	/**
	 * Unit tester
	 * @param args
	 */
	public static void main(String[] args){
		DataManagerModel model = new DataManagerModel();
		FileManager recipeFile = new FileManager();
		OpenRecipeViewController vc = new OpenRecipeViewController(model, recipeFile);
		vc.setVisible(true);
	} // main
} // class
